/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.groupbuy.multishop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.groupbuy.common.model.GroupOrder;
import com.yami.shop.groupbuy.common.service.GroupOrderService;
import com.yami.shop.security.multishop.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @author LGH
 * @date 2019-08-27 17:55:57
 */
@RestController
@RequestMapping("/group/order")
@Api(tags = "商家端团购订单接口")
public class GroupOrderController {

    @Autowired
    private GroupOrderService groupOrderService;

    @GetMapping("/page")
    @ApiOperation(value = "分页查找团购订单列表")
    public ServerResponseEntity<IPage<GroupOrder>> getGroupOrderPage(PageParam<GroupOrder> page, GroupOrder groupOrder) {
        return ServerResponseEntity.success(groupOrderService.page(page, new LambdaQueryWrapper<GroupOrder>()));
    }

    @GetMapping("/info/{groupOrderId}")
    @ApiOperation(value = "通过id查询团购订单活动")
    @ApiImplicitParam(name = "groupOrderId", value = "团购订单id", required = true, dataType = "Long")
    public ServerResponseEntity<GroupOrder> getById(@PathVariable("groupOrderId") Long groupOrderId) {
        GroupOrder groupOrder = groupOrderService.getById(groupOrderId);
        if (!Objects.equals(SecurityUtils.getShopUser().getShopId(), groupOrder.getShopId())) {
            throw new YamiShopBindException("yami.no.auth");
        }
        return ServerResponseEntity.success(groupOrder);
    }

    @PostMapping
    @ApiOperation(value = "新增团购订单")
    public ServerResponseEntity<Boolean> save(@RequestBody @Valid GroupOrder groupOrder) {
        return ServerResponseEntity.success(groupOrderService.save(groupOrder));
    }

    @PutMapping
    @ApiOperation(value = "修改团购订单")
    public ServerResponseEntity<Boolean> updateById(@RequestBody @Valid GroupOrder groupOrder) {
        return ServerResponseEntity.success(groupOrderService.updateById(groupOrder));
    }

    @DeleteMapping("/{groupOrderId}")
    @ApiOperation(value = "通过id删除团购订单活动")
    @ApiImplicitParam(name = "groupOrderId", value = "团购订单id", required = true, dataType = "Long")
    public ServerResponseEntity<Boolean> removeById(@PathVariable Long groupOrderId) {
        return ServerResponseEntity.success(groupOrderService.removeById(groupOrderId));
    }

}
